#include "NFCOfflineHandleModule.h"

#include "NFComm/NFPluginModule/NFPlatform.h"
#include "NFComm/NFPluginModule/NFIPluginManager.h"

#include "NFComm/NFPluginModule/NFINetModule.h"
#include "NFComm/NFPluginModule/NFINetServerModule.h"
#include "NFComm/NFPluginModule/NFIOfflineModule.h"
#include "NFComm/NFPluginModule/NFILogModule.h"

bool NFCOfflineHandleModule::Init()
{
    m_pOfflineModule = pPluginManager->FindModule<NFIOfflineModule>();
    m_pNetModule = pPluginManager->FindModule<NFINetServerModule>()->GetServerNetInfo(NF_ST_WORLD);
    m_pLogModule = pPluginManager->FindModule<NFILogModule>();
    return true;
}

bool NFCOfflineHandleModule::AfterInit()
{
    m_pNetModule->AddReceiveCallBack(NFMsg::EGMI_GAME_TO_WORLD_OFFLINE_DATA, this, &NFCOfflineHandleModule::OnOfflineDataProcess);
    return true;
}

bool NFCOfflineHandleModule::Execute()
{
    return true;
}

bool NFCOfflineHandleModule::BeforeShut()
{
    return true;
}

bool NFCOfflineHandleModule::Shut()
{
    return true;
}

bool NFCOfflineHandleModule::Finalize()
{
    return true;
}

void NFCOfflineHandleModule::OnOfflineDataProcess(const int nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
    CLIENT_MSG_PROCESS_NO_OBJECT(nMsgID, msg, nLen, NFMsg::GameUserOfflineData);

    NFGUID self = NFINetModule::PBToNF(xMsg.user_guid());
    NF_SHARE_PTR<NFMsg::GameUserOfflineData> pOfflineData = m_pOfflineModule->GetUserOfflineData(self);
    if (pOfflineData)
    {
        *pOfflineData = xMsg;
    }
    else
    {
        pOfflineData = NF_SHARE_PTR<NFMsg::GameUserOfflineData>(NF_NEW NFMsg::GameUserOfflineData());
        *pOfflineData = xMsg;
        m_pOfflineModule->AddUserOfflineData(self, pOfflineData);
    }
}